//
//  PushWebViewVC.m
//  ToYourHeartNews
//
//  Created by XJing on 16/1/26.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥. All rights reserved.
//

#import "PushWebViewVC.h"
#import "BlackManager.h"
#import "SetBlack.h"
#import "NewsDataBaseManager.h"
@interface PushWebViewVC ()
@property (nonatomic, strong)UIButton *likeBtn;
@end

@implementation PushWebViewVC
- (void)dealloc
{
    [_pathstr release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-newlisticon06"] style:UIBarButtonItemStylePlain target:self action:@selector(BackAction)] autorelease];
    self.likeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _likeBtn.frame = CGRectMake(0, 0, 30, 30);
//    [_likeBtn setTitle:@"收藏" forState:UIControlStateNormal];
    [_likeBtn setBackgroundImage:[UIImage imageNamed:@"iconfont-heart-2"] forState:UIControlStateNormal];
    [_likeBtn addTarget:self action:@selector(likeAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:_likeBtn];
    
    
    //webView
    UIWebView *webVC = [[UIWebView alloc]initWithFrame:CGRectMake(0, -62, self.view.frame.size.width, self.view.frame.size.height)];
    webVC.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:webVC];
    if ([BlackManager sharedInstance].isNight) {
        [webVC stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName(‘body‘)[0].style.background=‘#F6F7F3‘"];
    }
    //webView适应
    webVC.scalesPageToFit = YES;
    if (_pathstr.length != 0) {
        [webVC loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:_pathstr]]];
    }else{
        
        [webVC loadHTMLString:_pathhtml baseURL:[NSURL URLWithString:_pathhtml]];
    }
    
}
-(void)likeAction:(UIButton *)sender
{
    
    NSLog(@"收藏");
    //打开数据库
    NewsDataBaseManager *dbManager = [NewsDataBaseManager shareInstance];
    [dbManager openDB];
    //创建表
    [dbManager createTable];
    if (_likeBtn.selected) {
        //取消收藏
        [dbManager deleteNewsWithId:_model.NewsId];
        [_likeBtn setBackgroundImage:[UIImage imageNamed:@"iconfont-heart-2"] forState:UIControlStateNormal];
        NSLog(@"取消了收藏");
    }else{
        //收藏
        NSString *s = _model.title;
        NSLog(@"喜欢了1%@", s);
        //插入数据
        [dbManager insertNews:_model];
        [_likeBtn setBackgroundImage:[UIImage imageNamed:@"collection.png"] forState:UIControlStateNormal];
        NSMutableArray *arr = [dbManager select];
        AllDetailModel *m = arr.lastObject;
        _model.NewsId = m.NewsId;
        
    }
    _likeBtn.selected = !_likeBtn.selected;
   
}
-(void)BackAction
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
